package com.wangxiaonan.berry.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.berry.entity.BaiQiShi;
import com.wangxiaonan.berry.repository.BaiQiShiRepository;
import com.wangxiaonan.berry.service.BaiQiShiService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * @Author: wangxiaonan
 * @Date: 2019/7/2
 **/
@Service
@Transactional
public class BaiQiShiServiceImpl implements BaiQiShiService {

    @Value("${bai-qi-shi-partner-id}")
    private String partnerId;

    @Value("${bai-qi-shi-verify-key}")
    private String verifyKey;

    @Resource
    private BaiQiShiRepository baiQiShiRepository;

    @Override
    public String query(BaiQiShi baiQiShi) {
        Assert.notNull(baiQiShi, "Parameter is null!");
        if (StringUtils.isEmpty(baiQiShi.getVerifyKey())) {
            baiQiShi.setVerifyKey(verifyKey);
        }
        if (StringUtils.isEmpty(baiQiShi.getPartnerId())) {
            baiQiShi.setPartnerId(partnerId);
        }
        JSONObject jsonObject = baiQiShiRepository.query(baiQiShi);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }

        return jsonObject.toJSONString();
    }

    @Override
    public String queryHistory(BaiQiShi baiQiShi) {
        Assert.notNull(baiQiShi, "Parameter is null!");
        if (StringUtils.isEmpty(baiQiShi.getPartnerId())) {
            baiQiShi.setPartnerId(partnerId);
        }
        if (StringUtils.isEmpty(baiQiShi.getVerifyKey())) {
            baiQiShi.setVerifyKey(verifyKey);
        }
        JSONObject jsonObject = baiQiShiRepository.queryHistory(baiQiShi);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }

        return jsonObject.toJSONString();
    }
}
